#include <stdio.h>
#include <string.h>
#include <time.h>  // 包含时间处理函数
#include "../include/link_pub.h"

#define LOG_FILE "/home/chp/桌面/MyProject/txt_File/log.txt"

// 记录日志
void log_operation(const char* operation_type, const char* details) {
    FILE *file = fopen(LOG_FILE, "a");
    if (file != NULL) {
        time_t now = time(NULL);
        struct tm *local_time = localtime(&now);
        char time_buffer[26];
        strftime(time_buffer, 26, "%Y-%m-%d %H:%M:%S", local_time);

        fprintf(file, "[%s] 操作类型: %s, 详情: %s\n", time_buffer, operation_type, details);
        fclose(file);
    }
}


// 分析日志
void analyze_log() {
    char line[256];
    FILE *file = fopen(LOG_FILE, "r");
    if (file != NULL) {
        while (fgets(line, sizeof(line), file)) {
            printf("%s", line);
        }
        fclose(file);
    }
}

// 查询学生日志
void search_student_log(const char* student_id) {
    char line[256];
    FILE *file = fopen(LOG_FILE, "r");
    if (file != NULL) {
        while (fgets(line, sizeof(line), file)) {
            if (strstr(line, student_id)) {
                printf("%s", line);
            }
        }
        fclose(file);
    }
}

// 查询课程日志
void search_course_log(const char* course_id) {
    char line[256];
    FILE *file = fopen(LOG_FILE, "r");
    if (file != NULL) {
        while (fgets(line, sizeof(line), file)) {
            if (strstr(line, course_id)) {
                printf("%s", line);
            }
        }
        fclose(file);
    }
}
